<?php
/**
 * 京东控制
 */

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Libs\JdSdkLib;
use App\Model\JdCategoryModel;
use App\Model\JdJingxuanModel;

class JdController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
    }

    /** 精选产品页面 */
    public function jingxuan()
    {
        $this->data['title'] = '京东精选';
        $this->showSidebar();
        return view('backend.jd.jingxuan', $this->data);
    }

    /** 类别列表 */
    public function category()
    {
        $this->data['title'] = '京东类别';
        $this->showSidebar();
        return view('backend.jd.category', $this->data);
    }

    /** 类别列表 */
    public function categoryListPost()
    {
        $where = [];
        if (isset(self::$params['parent_id'])) {
            $where['parent_id'] = self::$params['parent_id'];
        }

        if (isset(self::$params['category_grade']) && self::$params['category_grade'] != -1) {
            $where['grade'] = self::$params['category_grade'];
        }

        if (!empty(self::$params['keyword'])) {
            $where[] = ['name', 'like', '%' . self::$params['keyword'] . '%'];
        }

        $offset = (self::$params['page_num'] - 1) * self::$params['page_size'];
        $list = JdCategoryModel::where($where)->orderBy('id', 'asc')->offset($offset)->limit(self::$params['page_size'])->get()->toArray();
        $total = JdCategoryModel::where($where)->count();
        return $this->responseSuccess('列表成功', ['total' => $total, 'list' => $list]);
    }

    /** 获取精选产品列表 */
    public function jingXuanPost()
    {
        $where = [];
        if (isset(self::$params['cid1']) && self::$params['cid1'] != -1) {
            $where['cid1'] = self::$params['cid1'];
        }

        if (isset(self::$params['cid2']) && self::$params['cid2'] != -1) {
            $where['cid2'] = self::$params['cid2'];
        }

        if (isset(self::$params['cid3']) && self::$params['cid3'] != -1) {
            $where['cid3'] = self::$params['cid3'];
        }

        if (!empty(self::$params['keyword'])) {
            $where[] = ['skuName' , 'like', '%' . self::$params['keyword'] . '%'];
        }

        $offset = (self::$params['page_num'] - 1) * self::$params['page_size'];
        $total = JdJingxuanModel::where($where)->count();
        $list = JdJingxuanModel::where($where)->offset($offset)->orderBy('id', 'asc')->limit(self::$params['page_size'])->get()->toArray();
        return $this->responseSuccess('成功', ['total' => $total, 'list' => $list]);
    }

    /** 同步京东类别 */
    public function syncCategoryPost()
    {
        $jdSdkLib = new JdSdkLib();
        $list = $jdSdkLib->getCategory(0, 0);
        $data = [
            'grade' => '',
            'parent_id' => 0,
            'jd_category_id' => 0,
            'name' => ''
        ];
        foreach ($list as $item) {
            $jdCategory = JdCategoryModel::where('jd_category_id', $item['id'])->first();
            if ($jdCategory->exists) {
                $jdCategory->grade = $item['grade'];
                $jdCategory->parent_id = $item['parentId'];
                $jdCategory->name = $item['name'];
                $jdCategory->save();
            } else {
                $data['name'] = $item['name'];
                $data['grade'] = $item['grade'];
                $data['parent_id'] = $item['parentId'];
                $data['jd_category_id'] = $item['id'];
                JdCategoryModel::insert($data);
            }

            $firstChildList = $jdSdkLib->getCategory($item['id'], 1);
            foreach ($firstChildList as $firstItem) {
                $jdCategory = JdCategoryModel::where('jd_category_id', $firstItem['id'])->first();
                if ($jdCategory) {
                    $jdCategory->grade = $firstItem['grade'];
                    $jdCategory->parent_id = $firstItem['parentId'];
                    $jdCategory->name = $firstItem['name'];
                    $jdCategory->save();
                } else {
                    $data['name'] = $firstItem['name'];
                    $data['grade'] = $firstItem['grade'];
                    $data['parent_id'] = $firstItem['parentId'];
                    $data['jd_category_id'] = $firstItem['id'];
                    JdCategoryModel::insert($data);
                }

                $secondChildList = $jdSdkLib->getCategory($firstItem['id'], 2);
                foreach ($secondChildList as $secondItem) {
                    $jdCategory = JdCategoryModel::where('jd_category_id', $secondItem['id'])->first();
                    if ($jdCategory) {
                        $jdCategory->grade = $secondItem['grade'];
                        $jdCategory->parent_id = $secondItem['parentId'];
                        $jdCategory->name = $secondItem['name'];
                        $jdCategory->save();
                    } else {
                        $data['name'] = $secondItem['name'];
                        $data['grade'] = $secondItem['grade'];
                        $data['parent_id'] = $secondItem['parentId'];
                        $data['jd_category_id'] = $secondItem['id'];
                        JdCategoryModel::insert($data);
                    }
                }
            }
        }

        return $this->responseSuccess();
    }

    /** 同步精选商品 */
    public function syncJingxuanPost()
    {
        set_time_limit(600);
        $jdSdkLib = new JdSdkLib();
        $eliteIdList = [1,2,10,15,22,23,24,25,26,27,28,30,31,32,33,34,40,41,108,109,110,112,125,129,130,153,210,238,247,249];
        foreach ($eliteIdList as $eliteId) {
            $totalCount = 0;
            $pageIndex = 0;
            $pageSize = 50;
            do {
                $pageIndex++;
                $result = $jdSdkLib->boutiqueGoods($eliteId, $pageIndex, $pageSize);
                if (!isset($result['totalCount'])) {
                    break;
                }
                $totalCount = $result['totalCount'];
                foreach ($result['data'] as $goods) {
                    $data['brandCode'] = $goods['brandCode'];
                    $data['brandName'] = $goods['brandName'];
                    $data['cid1'] = $goods['categoryInfo']['cid1'];
                    $data['cid1Name'] = $goods['categoryInfo']['cid1Name'];
                    $data['cid2'] = $goods['categoryInfo']['cid2'];
                    $data['cid2Name'] = $goods['categoryInfo']['cid2Name'];
                    $data['cid3'] = $goods['categoryInfo']['cid3'];
                    $data['cid3Name'] = $goods['categoryInfo']['cid3Name'];
                    $data['comments'] = $goods['comments'];
                    $data['goodCommentsShare'] = $goods['goodCommentsShare'];
                    $data['img'] = $goods['imageInfo']['imageList']['0']['url'];
                    $data['materialUrl'] = $goods['materialUrl'];
                    $data['shopId'] = $goods['shopInfo']['shopId'];
                    $data['shopName'] = $goods['shopInfo']['shopName'];
                    $data['skuId'] = $goods['skuId'];
                    $data['skuName'] = $goods['skuName'];
                    $data['pingouPrice'] = isset($goods['pinGouInfo']['pingouPrice']) ? $goods['pinGouInfo']['pingouPrice'] : 0;
                    $data['pingouTmCount'] = isset($goods['pinGouInfo']['pingouTmCount']) ? $goods['pinGouInfo']['pingouTmCount'] : 0;
                    $data['pingouUrl'] = isset($goods['pinGouInfo']['pingouUrl']) ? $goods['pinGouInfo']['pingouUrl'] : '';
                    $data['pingouStartTime'] = isset($goods['pinGouInfo']['pingouPrice']) ? $goods['pinGouInfo']['pingouPrice']/1000 : 0;
                    $data['pingouTmCount'] = isset($goods['pinGouInfo']['pingouTmCount']) ? $goods['pinGouInfo']['pingouTmCount']/1000 : 0;
                    $data['price'] = $goods['priceInfo']['price'];
                    $data['commission'] = $goods['commissionInfo']['commission'];
                    $data['commissionShare'] = $goods['commissionInfo']['commissionShare'];
                    $data['couponCommission'] = $goods['commissionInfo']['couponCommission'];
                    $filter['skuId'] = $data['skuId'];
                    $jingxuanModel = JdJingxuanModel::where($filter)->select('id')->first();
                    if ($jingxuanModel) {
                        $query['id'] = $jingxuanModel->id;
                        JdJingxuanModel::where($query)->update($data);
                    } else {
                        JdJingxuanModel::insert($data);
                    }
                }
            } while ($totalCount > $pageIndex * $pageSize);
        }

        return $this->responseSuccess();
    }

    /** 获取转链地址 */
    public function promotionPost()
    {
        $jdSdkLib = new JdSdkLib();
        $res = $jdSdkLib->promotion('item.jd.com/62953291938.html', 'lcm_test');
        var_dump($res);
    }

    public function test()
    {
        var_dump(1);
    }
}
